package com.minyifang.wcspf.work.ui.user.activities;

import android.content.Context;
import android.content.Intent;
import android.opengl.GLSurfaceView;
import android.text.TextUtils;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.minyifang.wcspf.R;
import com.minyifang.wcspf.common.base.BaseMvpActivity;
import com.minyifang.wcspf.common.config.user.UserManager;
import com.minyifang.wcspf.common.md5.SafeUtils;
import com.minyifang.wcspf.common.utils.LogUtil;
import com.minyifang.wcspf.common.utils.ToastHelper;
import com.minyifang.wcspf.work.dialog.AddressDialog;
import com.minyifang.wcspf.work.model.HttpRespond;
import com.minyifang.wcspf.work.ui.user.model.AddressBean;
import com.minyifang.wcspf.work.ui.user.model.AddressDialogBean;
import com.minyifang.wcspf.work.ui.user.presenter.AddAddressPresenter;
import com.minyifang.wcspf.work.ui.user.view.AddAddressView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by LuoYaWei on 2018/6/25.
 */
public class AddAddressActivity extends BaseMvpActivity<AddAddressView, AddAddressPresenter> implements AddAddressView {

    private static final String EXTRA_TYPE = "type";
    private static final String EXTRA_CONTENT = "content";

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_addAddress)
    TextView tv_addAddress;

    @BindView(R.id.edt_name)
    EditText edt_name;
    @BindView(R.id.edt_phone)
    EditText edt_phone;
    @BindView(R.id.tv_store)
    TextView tv_store;
   /* @BindView(R.id.tv_xiaoqu)
    TextView tv_xiaoqu;*/
    @BindView(R.id.edt_address_info)
    EditText edt_address_info;
    @BindView(R.id.cb_default)
    CheckBox cb_default;

    private String selectStoreId = "";
    private String selectXiaoQuId = "";

    private int type; //0新增 1 编辑
    private AddressBean mBean;

    private String addressId;//地址ID 编辑使用

    public static void start(Context context, int type, AddressBean bean) {
        Intent intent = new Intent(context, AddAddressActivity.class);
        intent.putExtra(EXTRA_TYPE, type);
        intent.putExtra(EXTRA_CONTENT, bean);
        context.startActivity(intent);
    }

    @Override
    protected AddAddressPresenter createPresenter() {
        return new AddAddressPresenter();
    }

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_add_address;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        if (intent != null) {
            type = intent.getIntExtra(EXTRA_TYPE, 0);
        }
        if (type == 1) {
            mBean = (AddressBean) intent.getSerializableExtra(EXTRA_CONTENT);
            edt_name.setText(mBean.getUserName());
            edt_phone.setText(mBean.getUserMobile());
            tv_store.setText(UserManager.getInstance().getStoreName());
//            tv_xiaoqu.setText(mBean.getVillageName());
            edt_address_info.setText(mBean.getAddress());
            cb_default.setChecked(mBean.getIsDefault().equals("1"));
            selectStoreId = mBean.getStoreId();
            selectXiaoQuId = mBean.getStoreAreaId();
            addressId = mBean.getID();

            tv_title.setText("编辑地址");
            tv_addAddress.setText("确认");
        } else {
            tv_store.setText(UserManager.getInstance().getStoreName());
            selectStoreId = UserManager.getInstance().getStoreId();
        }
    }

    /**
     * 选择门店
     */
   /* @OnClick(R.id.ll_store)
    void chooseStore() {
        mPresenter.getStoreList();
    }*/

    /**
     * 选择小区
     */
    @OnClick(R.id.ll_xiaoqu)
    void chooseXiaoQu() {
        if (tv_store.getText().toString().equals("")) {
            ToastHelper.getInstance(this).showWarn("请先选择门店");
            return;
        }
        mPresenter.getXiaoQuList(selectStoreId);
    }

    /**
     * 确认添加
     */
    @OnClick({R.id.tv_addAddress})
    void confirmAdd() {
        String edtName = edt_name.getText().toString();
        if (TextUtils.isEmpty(edtName)) {
            ToastHelper.getInstance(this).showWarn("请输入姓名");
            return;
        }
        String edtPhone = edt_phone.getText().toString();
        if (TextUtils.isEmpty(edtPhone)) {
            ToastHelper.getInstance(this).showWarn("请输入手机号");
            return;
        }
        /*if (TextUtils.isEmpty(selectStoreId)) {
            ToastHelper.getInstance(this).showWarn("请选择门店");
            return;
        }*/
       /* if (TextUtils.isEmpty(selectXiaoQuId)) {
            ToastHelper.getInstance(this).showWarn("请选择小区");
            return;
        }*/
        String edtAddressInfo = edt_address_info.getText().toString();
        if (TextUtils.isEmpty(edtAddressInfo)) {
            ToastHelper.getInstance(this).showWarn("请输入详细地址");
            return;
        }

        String idDef = cb_default.isChecked() ? "1" : "0";
        if (type == 0) {
            mPresenter.addAddress(edtName, edtPhone, selectStoreId, selectXiaoQuId, edtAddressInfo, idDef);
        }
        if (type == 1) {
            mPresenter.editAddress(addressId, edtName, edtPhone, selectStoreId, selectXiaoQuId, edtAddressInfo, idDef);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void showLoadingView() {
        showLoadingDialog();
    }

    @Override
    public void hideLoadingView() {
        hideLoadingDialog();
    }

    @Override
    public void onNetworkError() {
        hideLoadingDialog();
    }

    @Override
    public void showStoreList(HttpRespond<String> respond) {
        if (respond.result == 1) {

            if (respond.isaes == 1) {
                LogUtil.e("选择门店数据: " + SafeUtils.decrypt(respond.data));
                List<AddressDialogBean> list = new Gson().fromJson(SafeUtils.decrypt(respond.data), new TypeToken<List<AddressDialogBean>>() {
                }.getType());
                new AddressDialog.Builder(this).setList(list).setOnItemClickListener((dialog, position) -> {

                    tv_store.setText(list.get(position).getName());
                    selectStoreId = list.get(position).getID();
                    dialog.dismiss();

                }).build().show();
            }
        } else
            ToastHelper.getInstance(this).showWarn(respond.message);
    }

    @Override
    public void showXiaoQuList(HttpRespond<String> respond) {
        if (respond.result == 1) {

            if (respond.isaes == 1) {
                LogUtil.e("选择小区数据: " + SafeUtils.decrypt(respond.data));
                List<AddressDialogBean> list = new Gson().fromJson(SafeUtils.decrypt(respond.data), new TypeToken<List<AddressDialogBean>>() {
                }.getType());
                new AddressDialog.Builder(this).setList(list).setOnItemClickListener((dialog, position) -> {

//                    tv_xiaoqu.setText(list.get(position).getName());
                    selectXiaoQuId = list.get(position).getID();
                    dialog.dismiss();

                }).build().show();
            }
        } else
            ToastHelper.getInstance(this).showWarn(respond.message);
    }

    @Override
    public void showAddAddressRes(HttpRespond<String> respond) {
        if (respond.result == 1) {
            ToastHelper.getInstance(this).showSucceed(respond.message);
            finish();
        } else
            ToastHelper.getInstance(this).showWarn(respond.message);
    }

    @Override
    public void showEditAddressRes(HttpRespond<String> respond) {
        if (respond.result == 1) {
            ToastHelper.getInstance(this).showSucceed(respond.message);
            finish();
        } else
            ToastHelper.getInstance(this).showWarn(respond.message);
    }
}
